package com.example.pm;

import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends BaseActivity implements Runnable
{
    /*=========================列表显示所有已经开的房间信息，并且进行选择房间=======================
	 * 
	 * 
	 * */
	//private String content="";
	private ListView lv;
	//private String result="";
	private Handler handler;
    private String nicker;
  //网络通信相关的变量
  	public String CMD="";
  	public String content="";
  	public String result="";
	//private ListView lv;
	//private String CMD="";//send command to server
	private final byte[] keyBytes={0x11,0x22,0x4F,0x58,(byte)0x88,0x10,
		0x40,0x38,0x28,0x25,0x79,0x51,(byte)0xCB,(byte)0xDD,
		0x55,0x66,0x77,0x29,0x74,(byte)0x98,0x30,0x40,0x36,
		(byte)0xE2};//　24字节的密钥
	
	
	@Override
	public void run()
	{
		// TODO: Implement this method
		if(CMD=="TALK"){
		Net n=new Net();
		
		n.sendCC(CMD,content);
		n.start();
		//根据cmd不同得到result
	    result=n.getContentResult();
		Message m=handler.obtainMessage();
		m.what=0x01;
		handler.sendMessage(m);
		}
		else if(CMD=="CLS"){
			Net n=new Net();
			n.sendCmd(CMD);
			n.start();
			result="";
			Message m=handler.obtainMessage();
			m.what=0x03;
			handler.sendMessage(m);
		}
		else if(CMD=="REFRESHTALK"){
			
			while(true){
				Net n=new Net();
				n.sendCmd(CMD);
					n.start();
					result=n.getContentResult();
					Message m=handler.obtainMessage();
					m.what=0x02;
					handler.sendMessage(m);		
				try
				{
					Thread.sleep(5000);
				}
				catch (InterruptedException e)
				{}
			}
		}
		
		
	}
	

	@Override
	public void mainView()
	{
		// TODO: Implement this method
		setContentView(R.layout.activity_main);
		Button bt=(Button)findViewById(R.id.bt);
		final EditText et=(EditText)findViewById(R.id.et);
		lv=(ListView)findViewById(R.id.lv);
		if(Net.isNetworkAvailable(MainActivity.this)){
		refreshTalk();
		}
		//显示信息到主界面
		handler=new Handler(){
			public String[] toRes(String fromResult){
				String[] strs=null;
				if(fromResult.trim()!=null){
				byte[] encoded1=DES.hex2Bytes1(fromResult);
				byte[] dec=DES.decryptMode(keyBytes,encoded1);
				String temps=new String(dec);
				if(temps.contains("\n")){
					strs=temps.split("\n");
					if(strs!=null){
					   for(int i=0;i<strs.length;i++){
							strs[i]=strs[i].trim();
						    }
					}
				}
			   }
				return strs;
			}
			public void handleMessage(Message msg){
				if(result!=""&&CMD=="TALK"&&msg.what==0x01){
					String[] getStrs=toRes(result);
					lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,getStrs));
					et.setText("");						
					}			
				
				else if(result!=""&&CMD=="REFRESHTALK"&&msg.what==0x02){
					String[] getStrs=toRes(result);
					lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,getStrs));
				}
				else if(CMD=="CLS"&&msg.what==0x03){
					String strs[]=new String[]{""};
					lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,strs));				
				}
				super.handleMessage(msg);
			}
		};
		
		//send message
		/*
		et.setOnKeyListener(new OnKeyListener(){

				@Override
				public boolean onKey(View v, int keyCode, KeyEvent event)
				{
					// TODO: Implement this method
					if(keyCode==KeyEvent.KEYCODE_ENTER){
						content=et.getText().toString();
						//Toast.makeText(MainActivity.this,content,Toast.LENGTH_SHORT).show();
						
						say(content);
						return true;
					}
					return false;
				}
			});
			*/

		bt.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					// TODO: Implement this method
					content=et.getText().toString();
					say(content);
				}

			});	
		
	}
	public void say(String msgContent){
		if(msgContent.trim().equals("")||!Net.isNetworkAvailable(MainActivity.this)){
			Toast.makeText(MainActivity.this,"内容为空或者网络无法访问",Toast.LENGTH_SHORT).show();
		}
		else{
			nicker=getName();
			content=nicker+"："+content+"\n";
			CMD="TALK";
			byte[] encoded=DES.encryptMode(keyBytes,content.getBytes());
			content=new String(DES.byte2hex(encoded));
			Thread thread=new Thread(MainActivity.this);
			thread.start();
		}
		
	}
	
	
	
	public void refreshTalk()
	{
		// TODO: Implement this method
		CMD="REFRESHTALK";
		content="";
		Thread thread=new Thread(MainActivity.this);
		thread.start();
	}
	
	public void createRoom()
	{
		// TODO: Implement this method
	}

	
	public void clearRoom()
	{
		// clear all message
		CMD="CLS";
		content="";
		Thread thread=new Thread(MainActivity.this);
		thread.start();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// TODO: Implement this method
		getMenuInflater().inflate(R.menu.main,menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// 清空聊天记录
		if(item.getTitle().toString().trim().equals("清空")){
			clearRoom();
		    return true;
		}
		return false;
	}
	
}